layui.use(['form', 'layer', 'table', 'laydate', 'func', 'ax'], function () {
    var layer = layui.layer;
    var table = layui.table;
    var form = layui.form;
    var laydate = layui.laydate;
    var func = layui.func;
    var $ax = layui.ax;

    /**
     * 初始化表单，要加上，不然刷新部分组件可能会不加载
     */
    form.render();

    /**
     * 初始化表格
     */
    var CumulaDay = {
        tableId: "CumulaDayTable" //表格id
    };

    /**
     * 初始化表格的列
     */
    CumulaDay.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'dayNum', align: "center", sort: true, title: '连续打卡天数',
                templet: function(object){
                    return object.dayNum + '天';
                }
            },
            {field: 'goldNum', align: "center", sort: true, title: '奖励金币值',
                templet: function(object){
                    return "<span style='color:#FF5722'>" + object.goldNum + "</span>";
                }
            },
            {field: 'updateDate', align: "center", sort: true, title: '更新时间'},
            {field: 'userName', align: "center", sort: true, title: '更新者'},
            {align: 'center', toolbar: '#tableBar', title: "操作", minWidth: 80}
        ]];
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + CumulaDay.tableId,
        url: Feng.ctxPath + '/web/cumulaDay/list',
        page: true,
        limits: [10, 15, 20, 25, 50, 100],
        limit: 15,
        height: "full-98",
        cellMinWidth: 100,
        cols: CumulaDay.initColumn()
    });

    /**
     * 点击查询按钮
     */
    CumulaDay.search = function () {
        var queryData = {};
        queryData['dayNum'] = $("#dayNum").val();
        table.reload(CumulaDay.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 点击重置按钮
     */
    CumulaDay.reset = function () {
        $("#dayNum").val("");

        form.render();

        var queryData = {};
        queryData['dayNum'] = "";
        CumulaDay.search();
    };

    /**
     * 弹出添加对话框
     */
    CumulaDay.openAdd = function () {
        func.open({
            title: "添加连续打卡天数",
            maxmin: true,
            height: 400,
            width: '400px',
            content: Feng.ctxPath + '/web/cumulaDay/addOrEdit',
            tableId: CumulaDay.tableId
        });
    };

    /**
     * 弹出编辑对话框
     */
    CumulaDay.onEdit = function (data) {
        func.open({
            title: "编辑连续打卡天数",
            maxmin: true,
            height: 400,
            width: '400px',
            content: Feng.ctxPath + '/web/cumulaDay/addOrEdit?cumulaDayId=' + data.id,
            tableId: CumulaDay.tableId
        });
    };

    /**
     * 点击删除按钮
     *
     * @param data 点击按钮时候的行数据
     */
    CumulaDay.onDelete = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/web/cumulaDay/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(CumulaDay.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("cumulaDayId", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除(连续" + data.dayNum + "天)记录?", operation);
    };

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        CumulaDay.search();
    });

    // 重置按钮点击事件
    $('#btnReset').click(function () {
        CumulaDay.reset();
    });

    // 添加按钮点击事件
    $("#btnAdd").click(function(){
        CumulaDay.openAdd();
    })

    // 工具条点击事件
    table.on('tool(' + CumulaDay.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            CumulaDay.onEdit(data);
        } else if(layEvent === 'delete'){
            CumulaDay.onDelete(data);
        }
    });

});

$(function () {
    var panehHidden = false;
    if ($(this).width() < 769) {
        panehHidden = true;
    }
    $('#myContiner').layout({initClosed: panehHidden, west__size: 260});
});